package org.example.controller;

import org.jsoup.nodes.Document;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

public class MyPageProcessor1 implements PageProcessor {
    @Override
    public void process(Page page) {
//        使用原生的jsoup的api解析对象
        Html html = page.getHtml();
//        得到一个jsoup的document对象
        Document document = html.getDocument();
        String title = document.getElementsByTag("title").text();
//        传递给pipeline
        page.putField("title",title);
//        使用css 解析器解析页面
        String string = html.css("title","text").toString();
        page.putField("string",string);

        List<String> AllLink = html.css("link", "href").all();
        page.putField("link",AllLink);

//        使用xpath来解析  浏览器元素右键可以选择复制
        String xpath = html.xpath("/html/head/link[3]").get();
        page.putField("xpath",xpath);
    }

    @Override
    public Site getSite() {
        return Site.me();
    }

    public static void main(String[] args) {
        Spider.create(new MyPageProcessor1())
                .addUrl("http://139.186.167.6:49081/user/login?redirect=%2Fdata-center")
                .run();
    }
}
